﻿using System;
using System.Collections.Generic;
using System.Text;


namespace Magenic.CodeMastery.SharePoint.PMO
{
    public class HeaderComposer : Composer
    {
        public HeaderComposer(DateTime dayInFirstMonth, int monthsToDisplay)
            : base(dayInFirstMonth, monthsToDisplay) { }

        public String composeYearHeaderRow(int columnsPerMonth)
        {
            StringBuilder markup = new StringBuilder();
            int monthCount = firstDay.Month;
            markup.Append("<tr>");
            int displayCount = 0;
            int year = firstDay.Year;
            for (int monthIndex = 0; monthIndex < monthsToDisplay; ++monthIndex)
            {
                ++displayCount;
                if (firstDay.AddMonths(monthIndex).Month == 12 ||
                    monthIndex == (monthsToDisplay - 1))
                {
                    int colspan = columnsPerMonth * displayCount;
                    markup.Append(composeYearHeaderTD(colspan, year++));
                    displayCount = 0;
                }
            }
            markup.Append("</tr>");
            return markup.ToString();
        }

        private String composeYearHeaderTD(int colSpan, int year)
        {
            StringBuilder markup = new StringBuilder();
            markup.AppendFormat("<td colspan=\"{0}\" style=\"", colSpan);
            markup.Append("{border:1px solid black}\">");
            markup.AppendFormat("{0}</td>", year);
            return markup.ToString();
        }

        public String composeMonthHeaderRow(int columnsPerMonth)
        {
            StringBuilder markup = new StringBuilder();
            int monthCount = firstDay.Month;
            markup.Append("<tr>");
            for (int monthIndex = 0; monthIndex < monthsToDisplay; ++monthIndex)
            {
                markup.AppendFormat("<td colspan=\"{0}\" style=\"", columnsPerMonth);
                markup.Append("{border:1px solid black}\">");
                markup.AppendFormat("{0}</td>",
                    firstDay.AddMonths(monthIndex).ToString("MMMM"));
            }
            markup.Append("</tr>");
            return markup.ToString();
        }
    }
}